/*
	Copyright 2010 Kwok Ho Yin

   	Licensed under the Apache License, Version 2.0 (the "License");
   	you may not use this file except in compliance with the License.
   	You may obtain a copy of the License at

    	http://www.apache.org/licenses/LICENSE-2.0

   	Unless required by applicable law or agreed to in writing, software
   	distributed under the License is distributed on an "AS IS" BASIS,
   	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   	See the License for the specific language governing permissions and
   	limitations under the License.
*/

package com.hykwok.CameraEffect;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class ActivityMain extends Activity {
	private static final String TAG = "ActivityMain";
	
	private static final int MENU_ITEM_SETTING = 0;
	
	private MyCameraSurface		m_surface = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate >>>>>");
        
        super.onCreate(savedInstanceState);
        
        m_surface = new MyCameraSurface(this, false);
        
        // remove title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // full screen mode
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(m_surface);
        
        Log.d(TAG, "onCreate <<<<<");
    }
    
    @Override
	public void onStart() {
		Log.d(TAG, "onStart >>>>>");
		
		super.onStart();
		
		Log.d(TAG, "onStart <<<<<");
	}
	
	@Override
	public void onRestart() {
		Log.d(TAG, "onRestart >>>>>");
		
		super.onRestart();
		
		Log.d(TAG, "onRestart <<<<<");
	}
	
	@Override
	public void onResume() {
		Log.d(TAG, "onResume >>>>>");
		
		super.onResume();
		
		Log.d(TAG, "onResume <<<<<");
	}
	
	@Override
	public void onPause() {
		Log.d(TAG, "onPause >>>>>");
		
		super.onPause();
		
		if(m_surface.getControl() != null) {
			if(m_surface.getControl().IsCameraClosed() == false) {
				// camera is opened
				if(m_surface.getControl().IsPreviewRun() == true) {
					// stop preview now
					m_surface.getControl().stopPreview();
				}
			}
		}
		
		Log.d(TAG, "onPause <<<<<");
	}
	
	@Override
	public void onStop() {
		Log.d(TAG, "onStop >>>>>");
		
		super.onStop();
		
		Log.d(TAG, "onStop <<<<<");
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy >>>>>");
		
		super.onDestroy();
		
		m_surface.Close();
		
		Log.d(TAG, "onDestroy <<<<<");
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add(0, MENU_ITEM_SETTING, 0, R.string.menu_setting);
		return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    	case MENU_ITEM_SETTING:
    		Intent intent1 = new Intent(ActivityMain.this, ActivityCameraSetting.class);
    		startActivity(intent1);
    		break;
    	}
    	
    	return super.onOptionsItemSelected(item);
    }
}